<?php
declare (strict_types=1);

namespace app\admin\controller;

use addons\BuilderPlus\components\Button;
use addons\BuilderPlus\components\Column;
use addons\BuilderPlus\components\Search;
use addons\BuilderPlus\facade\Table;

class TradeOfflineOrder extends Base
{
    public function getOptions(): array
    {
        return [
            'pk' => 'id',
            'model' => \app\common\model\TradeOfflineOrder::class,
            'with' => ['trade', 'buyer', 'seller'],
            'append' => ['status_name'],
            'just' => false,
            'fast' => [
                'edit' => 'trade_offline_order/edit',
                'save' => '_save'
            ]
        ];
    }

    public function index(): string
    {
        return Table::init($this)
            ->setCheckbox(false)
            ->addColumns([
                ['id', 'ID', fn(Column $col) => $col->width(80)],
                ['sn', '交易单号', fn(Column $col) => $col->width(180)],
                ['trade_id', '挂单号', fn(Column $col) => $col->templet('{{d.trade?d.trade.sn:"-"}}')->width(180)],
                ['buyer_id', '买家', fn(Column $col) => $col->templet('#_buyerTpl')->minWidth(240)],
                ['seller_id', '卖家', fn(Column $col) => $col->templet('#_sellerTpl')->minWidth(240)],
                ['account', '交易账户'],
                ['amount', '交易数量'],
                ['price', '价格'],
                ['real_amount', '实际打款金额'],
                ['create_time', '交易时间', fn(Column $col) => $col->width(150)],
                ['status_name', '状态', fn(Column $col) => $col->fixed('right')]
            ])
            ->addRightButtons([
                'edit' => fn(Button $btn) => $btn->title("查看")
            ], 100)
            ->addSearch([
                ['create_time', '交易时间', fn(Search $search) => $search->type('date')],
                ['sn', '交易单号', fn(Search $search) => $search->condition('Like')],
                ['trade_id', '挂单号', fn(Search $search) => $search->relation('\app\common\model\TradeOffline', 'sn,id')],
                ['buyer_id', '买家编号/昵称/手机号', fn(Search $search) => $search->relation('\app\common\model\User', 'code|nickname|mobile,id')->condition('Like')],
                ['seller_id', '卖家编号/昵称/手机号', fn(Search $search) => $search->relation('\app\common\model\User', 'code|nickname|mobile,id')->condition('Like')],
            ])
            ->enableExport()
            ->fetch();
    }
}